package com.sec.android.app.myfiles.module.preview;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.filelist.FileListFragment;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.provider.DbTableInfo;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;

public class PreviewCompressListFragment extends FileListFragment {

    private static ArrayList<WeakReference<PreviewCompressListFragment>> sInstanceList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (NavigationManager.isPossibleToActionMode(getProcessId())) {
            FileRecord curRecord = getNavigationInfo().getCurRecord();
            if (curRecord != null) {
                if (curRecord.getStorageType() == FileRecord.StorageType.Compress) {
                    FileRecord archiveRecord = ((CompressFileRecord) curRecord).getArchiveRecord();
                    if (archiveRecord != null) {
                        MyFilesFacade.previewCompress(getProcessId(), getActivity(), this, archiveRecord);
                    }
                }
            }
        } else {
            Log.d(this, "Unable to preview compressed file.");
            Activity a = getActivity();
            if (a != null) {
                Toast.makeText(a, R.string.unable_to_open_file, Toast.LENGTH_SHORT).show();
                a.onBackPressed();
            }
        }
        addInstance();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        selectAll(true);
    }

    @Override
    public void onDestroy() {
        removeInstance();
        if (sInstanceList.isEmpty()) {
            DbTableInfo tableInfo = PreviewCompressDbTableInfo.getInstance();
            mContext.getContentResolver().delete(Uri.parse(tableInfo.getUri()), null, null);
        }
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Activity activity = getActivity();
        if (activity != null) {
            activity.invalidateOptionsMenu();
        }
    }

    @Override
    protected boolean _onItemClick(FileRecord record) {
        // DO NOTHING. Always edit mode.
        return false;
    }

    private void addInstance() {
        Iterator<WeakReference<PreviewCompressListFragment>> it = sInstanceList.iterator();
        while (it.hasNext()) {
            if (it.next().get() == null) {
                it.remove();
            }
        }
        sInstanceList.add(new WeakReference<>(this));
    }

    private void removeInstance() {
        Iterator<WeakReference<PreviewCompressListFragment>> it = sInstanceList.iterator();
        PreviewCompressListFragment fragment;
        while (it.hasNext()) {
            fragment = it.next().get();
            if (fragment == null || this.equals(fragment)) {
                it.remove();
            }
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    public void onLoadFinished() {
        super.onLoadFinished();
        updateActionBarMenu();
    }

}
